//
//  PersonalViewController.m
//  Findif
//
//  Created by Mac on 2018/11/5.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "PersonalViewController.h"
#import <JMessage/JMessage.h>
#import "ChatRoomViewController.h"
#import "ZJImageMagnification.h"
#import "PersonalHeaderView.h"
#import "SquareTableViewCell.h"
#import "SquareModel.h"
#import "SquareDetailViewController.h"
#import "MapViewController.h"
#import "GroupInfoViewController.h"
#import "PersonalAlbumViewController.h"

@interface PersonalViewController ()<UITableViewDelegate,UITableViewDataSource,PersonalHeaderViewDelegate,SquareTableViewCellDelegate>

//@property (nonatomic, weak)UIImageView * topImageView;
//@property (nonatomic, weak)UIImageView * iconImageView;
//@property (nonatomic, strong)UILabel * nameLabel;
//@property (nonatomic, weak)UIImageView * localImageView;
//@property (nonatomic, weak)UILabel * localLabel;
//@property (nonatomic, strong)UIButton * attentionButton;
//@property (nonatomic, strong)UIButton * msgButton;
//@property (nonatomic, strong)UILabel * attentionValueLabel;
//@property (nonatomic, strong)UILabel * fansValueLabel;
////@property (nonatomic, strong)UILabel * comeValueLabel;
//@property (nonatomic, strong)UILabel * attentionLabel;
//@property (nonatomic, strong)UILabel * fansLabel;
////@property (nonatomic, strong)UILabel * comeLabel;
@property (nonatomic, copy)NSString * telStr;
//
//@property (nonatomic, strong)UICollectionView * collectionView;
//@property (nonatomic, weak)UILabel * showLabel;
//@property (nonatomic, strong)UIImageView * oneImageView;
//@property (nonatomic, strong)UIImageView * twoImageView;
//@property (nonatomic, strong)UIImageView * threeImageView;
//@property (nonatomic, weak)UIImageView * albumImageView;
//@property (nonatomic, weak)UILabel * albumLabel;
//@property (nonatomic, weak)UILabel * albumCountLabel;
//@property (nonatomic, weak)UIButton * moreButton;
//@property (nonatomic, weak)UIImageView * dynamicImageView;
//@property (nonatomic, weak)UILabel * dynamicLabel;
//@property (nonatomic, weak)UILabel * dynamicCountLabel;

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)PersonalHeaderView * headerView;
@property (nonatomic, strong)NSArray <SquareModel *> * listArray;
@property (nonatomic, strong)NSDictionary * dict;
@property (nonatomic, strong)NSArray * array;


@end

@implementation PersonalViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"他的资料";
    self.view.backgroundColor = ColorWithRGB(0xf9f9f9);
//    [self setupNav];
    [self setupUI];
}


- (void)loadData{

    NSString * user_id = DEF_GET_OBJECT(@"id");

    [[ZRNetworkTool sharedTool] getUserInfoResultWithUser_id:self.ID WithExamine_user_id:user_id WithBlock:^(BOOL success, id responseData) {

        if (success) {

            self.dict = responseData[@"data"];
            self.telStr = [NSString stringWithFormat:@"%@",self.dict[@"phone"]];
        }
    }];

    [[ZRNetworkTool sharedTool] getUserPhotoAlbumWithUser_id:self.ID ResultWithBlock:^(BOOL success, id responseData) {

        if (success) {

            self.array = responseData[@"data"];
        }
    }];

    NSDictionary * dict = @{@"user_id":self.ID,@"view_user_id":user_id};
    
    [[ZRNetworkTool sharedTool] getMyDynamicDict:dict WithBlock:^(BOOL success, id responseData) {
        
        //        NSLog(@"res===%@",responseData);
        if (success) {
            
            self.listArray = [NSArray yy_modelArrayWithClass:[SquareModel class] json:responseData[@"data"]];
        }
    }];
}

- (void)setListArray:(NSArray<SquareModel *> *)listArray{
    
    _listArray = listArray;
    
//    self.headerView.dynamicCountLabel.text = [NSString stringWithFormat:@"%lu个动态",(unsigned long)self.listArray.count];
    self.headerView.count = [NSString stringWithFormat:@"%lu",listArray.count];
    [self.tableView reloadData];
}


- (void)setArray:(NSArray *)array{
    
    _array = array;
    
    self.headerView.array = array;
    [self.tableView reloadData];
}


- (void)setDict:(NSDictionary *)dict{
    
    _dict = dict;
    
    self.headerView.dict = dict;
    [self.tableView reloadData];
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topHeight, kScreenW, kScreenH - topHeight) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF8F9FA);
//    tableView.rowHeight = 120 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.headerView =  [[PersonalHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 455 * SCREENH6)];
    // 设置header
    self.headerView.delegate = self;
    
    tableView.tableHeaderView = self.headerView;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[SquareTableViewCell class] forCellReuseIdentifier:@"PersonalSquareTableViewCell"];
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//
//        [self loadData];
//    }];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SquareTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行，而不是从cell重用队列中取出
    if (cell == nil) {
        cell = [[SquareTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonalSquareTableViewCell"];
        
    }
    
    cell.delegate = self;
    cell.model = self.listArray[indexPath.row];
    cell.deleteButton.hidden = YES;
    cell.muneButton.hidden = YES;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    SquareDetailViewController * datailVC = [SquareDetailViewController new];
    
    SquareModel * model = self.listArray[indexPath.row];
    
    datailVC.model = model;
    
    [self.navigationController pushViewController:datailVC animated:YES];
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 使用缓存行高，避免计算多次
    SquareModel * model = self.listArray[indexPath.row];
    return model.rowHeight;
}


- (UILabel *)setupLabelWithTitle:(NSString *)title WithType:(NSString *)type{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    if ([type isEqualToString:@"big"]) {
        
        label.textColor = ZRBlackColor;
        label.font = [UIFont boldSystemFontOfSize:16 * SCREENH6];
    }else if ([type isEqualToString:@"small"]){
        
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    }
    
    
    label.textAlignment = NSTextAlignmentCenter;
    
    return label;
}

- (void)attentionPerson{
    
    [[ZRNetworkTool sharedTool] cancelAttentionUserUser_id:self.ID ResultWithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      [self loadData];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}


- (void)clickTopButton:(UIButton *)button{
    
    if (button.tag == 2002) {
        
        NSString * user_id = DEF_GET_OBJECT(@"id");
        
        if ([self.ID isEqualToString:user_id]) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"亲，不能和自己聊天哦~"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            [JMSGConversation createSingleConversationWithUsername:self.telStr completionHandler:^(id resultObject, NSError *error) {
                
                if (error == nil) {
                    HiddenBar;
                    ChatRoomViewController * roomVC = [ChatRoomViewController new];
                    
                    roomVC.conversation = resultObject;
                    
                    [self.navigationController pushViewController:roomVC animated:YES];
                    
                } else {
                    
                }
            }];
        }
        
        
    }else if (button.tag == 2001){
        
        button.highlighted = NO;
        
        if (button.selected) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"确定不再关注此人？"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      [self attentionPerson];
                                                                  }];
            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                                 handler:^(UIAlertAction * action) {
                                                                     
                                                                 }];
            
            [alert addAction:defaultAction];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            [[ZRNetworkTool sharedTool] attentionUseroWithUser_id:self.ID ResultWithBlock:^(BOOL success, id responseData) {
                
                if (success) {
                    
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                   message:responseData[@"message"]
                                                                            preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                          handler:^(UIAlertAction * action) {
                                                                              
                                                                              [self loadData];
                                                                          }];
                    
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];
                }else{
                    
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                   message:responseData[@"message"]
                                                                            preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                          handler:^(UIAlertAction * action) {
                                                                              
                                                                          }];
                    
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];
                }
            }];
        }
    }
}
- (void)didMoreButton{
    
    HiddenBar;
    PersonalAlbumViewController * personVC = [PersonalAlbumViewController new];
    
    personVC.ID = self.ID;
    
    [self.navigationController pushViewController:personVC animated:YES];
}
- (void)clickIconImageWith:(UIImageView *)imageView{
    
    [ZJImageMagnification scanBigImageWithImageView:imageView alpha:1];
}

- (void)clickLocalButtonLocal:(NSString *)local WithDynamic_latitude:(NSString *)latitude WithDynamic_longitude:(NSString *)longitude{
    
    HiddenBar;
    
    MapViewController * mapVC = [MapViewController new];
    
    mapVC.merchants_latitude = latitude;
    mapVC.merchants_longitude = longitude;
    mapVC.merchants_local = local;
    
    [self.navigationController pushViewController:mapVC animated:YES];
    
}

- (void)clickGroupLabelWithCell:(SquareTableViewCell *)cell{
    
    NSDictionary * dict = @{@"id":cell.model.group_id};
    [[ZRNetworkTool sharedTool] checkGroupStatusWithDict:dict WithBlock:^(BOOL success, id responseData) {
        
        //        NSLog(@"res===%@",responseData);
        if (success) {
            
            HiddenBar;
            GroupInfoViewController * infoVC = [GroupInfoViewController new];
            
            infoVC.group_id = cell.model.group_id;
            
            [self.navigationController pushViewController:infoVC animated:YES];
            
            ShowBar;
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}


@end
